package com.anjho.pojo;

import java.io.Serializable;
import java.util.Date;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

@Entity
@Table(name = "person")
@NamedQueries({
		@NamedQuery(name = "Person.findAll", query = "SELECT p FROM Person p"),
		@NamedQuery(name = "Person.findByDocument", query = "SELECT p FROM Person p WHERE p.document = :document")
})
public class Person implements Serializable {
	
	private static final long serialVersionUID = -1763248351730477102L;
	
	public Person(){
		this.setAddress(new Address());
		this.setPersonType(new PersonType());
		this.setBirthDate(new Date());
	}

	@Id
	@Column(name = "document", length = 14)
	@OneToMany(mappedBy="person", cascade = {CascadeType.PERSIST})
	private String document;

	@Column(name = "name", nullable = false, length = 80)
	private String name;

	@ManyToOne
	@JoinColumn(name = "adcode", nullable = false)
	private Address address;
	
	@ManyToOne
	@JoinColumn(name = "ptcode", nullable = false)
	private PersonType personType;

	@Column(name = "addressNumber")
	private int addressNumber;

	@Column(name = "complement", length = 50)
	private String complement;

	@Temporal(TemporalType.DATE)
	@Column(name = "birthDate", nullable = false)
	private Date birthDate;

	@Column(name = "phone1", length = 12)
	private String phone1;

	@Column(name = "phone2", length = 12)
	private String phone2;

	@Column(name = "email", length = 80)
	private String email;

	public String getDocument() {
		return document;
	}

	public void setDocument(String document) {
		this.document = document;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Address getAddress() {
		return address;
	}

	public void setAddress(Address address) {
		this.address = address;
	}

	public PersonType getPersonType() {
		return personType;
	}

	public void setPersonType(PersonType personType) {
		this.personType = personType;
	}

	public int getAddressNumber() {
		return addressNumber;
	}

	public void setAddressNumber(int addressNumber) {
		this.addressNumber = addressNumber;
	}

	public String getComplement() {
		return complement;
	}

	public void setComplement(String complement) {
		this.complement = complement;
	}

	public Date getBirthDate() {
		return birthDate;
	}

	public void setBirthDate(Date birthDate) {
		this.birthDate = birthDate;
	}

	public String getPhone1() {
		return phone1;
	}

	public void setPhone1(String phone1) {
		this.phone1 = phone1;
	}

	public String getPhone2() {
		return phone2;
	}

	public void setPhone2(String phone2) {
		this.phone2 = phone2;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result
				+ ((document == null) ? 0 : document.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Person other = (Person) obj;
		if (document == null) {
			if (other.document != null)
				return false;
		} else if (!document.equals(other.document))
			return false;
		return true;
	}

}
